package cn.com.dhc.danlu.activity.coupon;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.coupon.callback.OnEnvelopLogicCallback;
import cn.com.dhc.danlu.fragment.base.BaseV4Fragment;
import cn.com.dhc.danlu.util.CommonConst.CouponConst;
import cn.com.dhc.danlu.widget.DanluCouponRecyclerView;
import cn.com.dhc.danlu.widget.couponview.filterview.FilterType;
import cn.com.dhc.danlu.widget.loadlayer.LoaddingLayout;
import cn.com.hd.mall.web.webservices.entity.freedom.entity.coupon.CouponEntity;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-09-27
 * Time: 16:42
 * Description: 可用经销商红包 & 不可用经销商红包
 */
public class CouponableFragment extends BaseV4Fragment {
    private LoaddingLayout llLoading;
    private DanluCouponRecyclerView xrvCoupon;

    private OnEnvelopLogicCallback mCallback;

    public static CouponableFragment newInstance(boolean isCanUse) {
        CouponableFragment fragment = new CouponableFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(CouponConst.DEALERS_INTENT_USE, isCanUse);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mCallback = (OnEnvelopLogicCallback) context;
        } catch (Exception e) {
            throw new RuntimeException("host act must imp OnEnvelopLogicCallback");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dl_fragment_coupon_used, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        showLoaddingView();
    }

    private void initView() {
        xrvCoupon = (DanluCouponRecyclerView) getView().findViewById(R.id.xrv_coupon);
        llLoading = (LoaddingLayout) getView();
        llLoading.setEmptyPic(R.drawable.ic_empty_jxsenvelop);
        llLoading.changeEmptyViewTips("当前无" + (getArguments().getBoolean(CouponConst.DEALERS_INTENT_USE) ? "可用" : "不可用") + "红包");
        llLoading.setRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLoaddingView();
                callHostActToReload();
            }
        });

        xrvCoupon.setPullRefreshEnabled(false);
        xrvCoupon.setLoadingMoreEnabled(false);
        xrvCoupon.setLayoutManager(new LinearLayoutManager(getContext()));
        xrvCoupon.setHasFixedSize(true);
    }

    private void showLoaddingView() {
        llLoading.showLoaddingView();
    }

    private void callHostActToReload() {
        mCallback.fetchCouponList();
    }

    public void refreshView(List<CouponEntity> datas, boolean isUsable) {
        if (datas.isEmpty()) {
            // 当前界面不存在上拉刷新、下拉刷新交互
            llLoading.showEmptyView(isUsable ? "暂无可用红包" : "暂无不可用红包");
            return;
        }
        llLoading.showContentView();
        DealersCouponUsedAdapter adapter = new DealersCouponUsedAdapter(getActivity(), FilterType.dealers_envelop_use, isUsable);
        adapter.setCheckChangeCallback(mCallback);
        xrvCoupon.setAdapter(adapter);
        adapter.refreshAllData(datas);
    }

    public void showErrorView(String errorInfo) {
        llLoading.showErrorView(errorInfo);
    }
}